package com.oocl.admin_platform.servlet;

import java.awt.image.RescaleOp;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.net.aso.r;

import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.MerchantService;
import com.oocl.admin_platform.service.factory.ServiceFactory;
import com.oocl.admin_platform.util.Base64Unit;
import com.oocl.admin_platform.vo.MerchantVO;

/**
 * Servlet implementation class ImageServlet
 */
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantService merchantService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    
    
    @Override
    public void init() throws ServletException {
    	merchantService = ServiceFactory.getMerchantServiceInstance();

    }



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		merchantService = ServiceFactory.getMerchantServiceInstance();  
		String id = request.getParameter("id");
		String type=request.getParameter("type");
		Merchant merchant=merchantService.findById(id);
		OutputStream outputStream = response.getOutputStream();
		if ("idcard".equals(type)) {
			String idImagePath=(String) request.getServletContext().getAttribute("path")+"/"+(String) request.getServletContext().getAttribute("idCardSrc")+"/" +merchant.getUname() + "1." + request.getServletContext().getAttribute("imageType");
			InputStream inputStream=new FileInputStream(new File(idImagePath));
			byte[]b=new byte[inputStream.available()];
			inputStream.read(b);
			outputStream.write(b);
		}
		if ("portrait".equals(type)) {
			String portraitImagePath=(String) request.getServletContext().getAttribute("path")+"/"+(String) request.getServletContext().getAttribute("portraitSrc")+"/"+merchant.getUname() +"2."+request.getServletContext().getAttribute("imageType");
			InputStream inputStream=new FileInputStream(new File(portraitImagePath));
			byte[]b=new byte[inputStream.available()];
			inputStream.read(b);
			outputStream.write(b);
		}
		outputStream.flush();
		outputStream.close();
		
	}

}
